import { setToken, getToken, localSave } from '@/utils'
import { login, getUserInfo, getMenus, signOut} from '../../api/sys/user';
export default {
    state: {
        userName: '', // 用户名
        token: getToken(), // token
        hasGetInfo: false, // 是否已经登录
        avatorImgPath: '', // 用户头像
        userId: '', // 用户id
        name: '',
        menus: []
    },
    mutations: {
        setAvator (state, avatorPath) {
            state.avatorImgPath = avatorPath
        },
        setUserId (state, id) {
            state.userId = id
        },
        setUserName (state, username) {
            state.userName = username
        },
        setName (state, name) {
            state.name = name
        },
        setToken (state, token) {
            state.token = token
            setToken(token)
        },
        setHasGetInfo (state, status) {
            state.hasGetInfo = status
        },
        setMenus (state, menus) {
            localSave('menus' , JSON.stringify(menus))
            state.menus = menus
        }
    },
    getters: {
        getUsername: state => state.userName,
        getAvator: state => state.avatorImgPath
    },
    actions: {
        // 登录
        handleLogin ({ commit }, { userName, password }) {
            userName = userName.trim()
            return new Promise((resolve, reject) => {
                try {
                    login({ userName, password }).then(res => {
                        const { token } = res.data
                        commit('setToken',token)
                        resolve(res)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 获取用户信息
        handleUserInfo ({ commit }) {
            return new Promise((resolve, reject) => {
                try {
                    getUserInfo().then(res => {
                        const data = res.data
                        commit('setAvator', data.avator)
                        commit('setUserName', data.username)
                        commit('setName', data.name)
                        commit('setUserId', data.userId)
                        commit('setHasGetInfo', true)
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 获取菜单信息
        handleMenus ({ commit }) {
            return new Promise((resolve, reject) => {
                try {
                    getMenus().then(res => {
                        const data = res.data
                        commit('setMenus', data)
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        // 退出
        logOut ({ commit }) {
            return new Promise((resolve, reject) => {
                try {
                    signOut().then(res => {
                        commit('setToken', "")
                        resolve(res)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        }
    }
}
